<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="catalog1Val" placeholder="一级分类" style="width: 110px" class="filter-item" @change="changeCatalog1">
        <el-option v-for="item in catalog1" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>

      <el-select v-model="catalog2Val" placeholder="二级分类" style="width: 110px" class="filter-item" @change="changeCatalog2">
        <el-option v-for="item in catalog2" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>

      <el-select v-model="catalog3Val" placeholder="三级分类" style="width: 110px" class="filter-item" @change="changeCatalog3">
        <el-option v-for="item in catalog3" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>

      <router-link :to="'/spu/create'">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit">
          新增
        </el-button>
      </router-link>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="标题">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="默认图片" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.imgUrl" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column label="原价" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.originPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="现价" width="110px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="简述" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <!--        <el-table-column label="状态" class-name="status-col" width="100">-->
      <!--          <template slot-scope="{row}">-->
      <!--            <el-tag :type="row.status | statusStyle">-->
      <!--              {{ row.status | statusFilter }}-->
      <!--            </el-tag>-->
      <!--          </template>-->
      <!--        </el-table-column>-->
      <el-table-column label="创建日期" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createDate | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <router-link :to="'/sku/list/'+scope.row.id">
            <el-button round size="small">
              SKU列表
            </el-button>
          </router-link>

          <router-link :to="'/spu/edit/'+scope.row.id">
            <el-button size="small" icon="el-icon-edit" circle>

            </el-button>
          </router-link>

          <el-button type="danger" size="small" icon="el-icon-delete" circle @click="handleDelete(scope.$index, scope.row.id)">

          </el-button>
        </template>

      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.curPageNum" :limit.sync="listQuery.pageSize" @pagination="getSpuByParam" />

  </div>
</template>

<script>
import { getCatalog1, getCatalog2, getCatalog3 } from '@/api/catalog'
import { getSpuListByCatalog3Id, getSpuByParam, deleteSpu } from '@/api/spu'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Spu',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      catalog1: null,
      catalog2: null,
      catalog3: null,
      catalog1Val: '',
      catalog2Val: '',
      catalog3Val: '',
      tableKey: 0,
      list: null,
      listQuery: {
        curPageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    this.getCatalog1()
    this.getSpuByParam()
  },
  methods: {
    getCatalog1() {
      this.listLoading = true
      getCatalog1().then(r1 => {
        this.catalog1 = r1.data
        this.listLoading = false
      })
    },

    changeCatalog1(e) {
      this.listLoading = true
      getCatalog2(e).then(r1 => {
        this.catalog2 = r1.data
        this.catalog3 = null
        this.catalog2Val = ''
        this.catalog3Val = ''
        this.listLoading = false
      })

      this.listQuery.catalog1Id = e
      this.listQuery.catalog2Id = null
      this.listQuery.catalog3Id = null
      this.getSpuByParam()
    },

    changeCatalog2(e) {
      this.listLoading = true
      getCatalog3(e).then(r1 => {
        this.catalog3 = r1.data
        this.catalog3Val = ''
        this.listLoading = false
      })
      this.listQuery.catalog1Id = null
      this.listQuery.catalog2Id = e
      this.listQuery.catalog3Id = null
      this.getSpuByParam()
    },

    changeCatalog3(e) {
      // 加载数据
      this.listQuery.catalog1Id = null
      this.listQuery.catalog2Id = null
      this.listQuery.catalog3Id = e
      this.getSpuByParam()
    },

    getSpuByParam(){
      this.listLoading = true
      getSpuByParam(this.listQuery).then(r1 => {
        console.log("返回：",r1.data)
        this.list = r1.data.list
        this.total = r1.data.total
        this.listLoading = false
      })
    },

    getList(id) {
      this.listLoading = true
      getSpuListByCatalog3Id(id).then(r1 => {
        this.list = r1.data
        this.listLoading = false
      })
    },

    handleDelete(index,spuId) {
      this.$confirm('确定要删除该spu?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteSpu(spuId)
          this.list.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => { console.error(err) })
    },

  }
}
</script>
